﻿#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;

#endregion

namespace Library
{
    public partial class MainForm : Form
    {
        User m_user = null;
        UserControlLogin m_loginForm;
        UserControlFactory m_userControlFactory;

        public MainForm()
        {
            InitializeComponent();
            ShowMainFormPanel(false);
            initLoginForm();
            initButtonClick();
        }

        private void initLoginForm()
        {
            try
            {
                m_loginForm = new UserControlLogin(this);
                m_loginForm.Dock = DockStyle.Fill;
                this.Controls.Add(m_loginForm);
            }
            catch (Exception x)
            {
                throw x;
            }
        }

        private void initButtonClick()
        {
            treeNodeUser.AfterSelect += new TreeViewEventHandler(treeNodeUser_AfterSelect);
            treeNodeBookCategory.AfterSelect += new TreeViewEventHandler(treeNodeBookCategory_AfterSelect);
            treeNodeMemberType.AfterSelect += new TreeViewEventHandler(treeNodeMemberType_AfterSelect);
            treeNodeBookShelf.AfterSelect += new TreeViewEventHandler(treeNodeBookShelf_AfterSelect);
            treeNodeMemberClass.AfterSelect += new TreeViewEventHandler(treeNodeMemberClass_AfterSelect);
            treeNodeMember.AfterSelect += new TreeViewEventHandler(treeNodeMember_AfterSelect);
            treeNodeBook.AfterSelect += new TreeViewEventHandler(treeNodeBook_AfterSelect);
            treeNodeBookStockAdjustment.AfterSelect += new TreeViewEventHandler(treeNodeBookStockAdjustment_AfterSelect);
            treeNodeBookBorrow.AfterSelect += new TreeViewEventHandler(treeNodeBookBorrow_AfterSelect);
            treeNodeBookReturn.AfterSelect += new TreeViewEventHandler(treeNodeBookReturn_AfterSelect);
        }

        void treeNodeBookReturn_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOOK_RETURN);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeBookBorrow_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOOK_BORROW);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeBookStockAdjustment_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOOK_STOCK_ADJUSTMENT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeBook_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOOK);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeMember_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.MEMBER);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeMemberClass_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.MEMBER_CLASS);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeBookShelf_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOOK_SHELF);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeMemberType_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.MEMBER_TYPE);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeBookCategory_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOOK_CATEGORY);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeUser_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.USER);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void loadUserControl(string p)
        {
            splitContainer1.Panel2.Controls.Clear();
            m_userControlFactory.GetUserControl(p).Dock = DockStyle.Fill;
            splitContainer1.Panel2.Controls.Add(m_userControlFactory.GetUserControl(p));
        }

        internal void ShowMainFormPanel(bool show)
        {
            panelMainForm.Visible = show;
        }

        public User User
        {
            get { return m_user; }
            set
            {
                m_user = value;
                m_user.SESSION_ID = this.Session.SessionID;
                textBoxUserName.Text = m_user.Name;
                splitContainer1.Panel2.Controls.Clear();
                m_userControlFactory = new UserControlFactory(m_user, this);
                initButtonPermission();
            }
        }

        private void initButtonPermission()
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}